package com.project.market.service.impl;

import com.project.market.db.domain.MarketKeywordExample;
import com.project.market.db.domain.MarketUser;
import com.project.market.db.domain.MarketUserExample;
import com.project.market.db.mapper.MarketUserMapper;
import com.project.market.service.AdminStartService;
import com.project.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class AdminStartServiceImpl implements AdminStartService {
    @Override
    public Map<Object, Object> user() {

        SqlSession session = MybatisUtil.getSession();
        MarketUserMapper mapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketUser> marketUsers = mapper.selectByExample(new MarketKeywordExample());

        Map<String,Integer> map = new HashMap<>();

        for (MarketUser marketUser : marketUsers) {
             String day = marketUser.getAddTime().toString().substring(0,marketUser.getAddTime().toString().indexOf("T"));
             if(map.containsKey(day)){
                    map.put(day,map.get(day)+1);
            }else {
                 map.put(day,1);
             }
        }
        String[] columns = {"day","users"};
        List<Map<String, Object>> list = new ArrayList<>();
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            Map<String,Object> bitMap = new HashMap<>();
            bitMap.put(columns[0],entry.getKey());
            bitMap.put(columns[1],entry.getValue());
            list.add(bitMap);
        }

        Map<Object, Object> map1 = new HashMap<>();
        map1.put("columns",columns);
        map1.put("rows",list);
        session.commit();
        session.close();
        return map1;
    }
}
